<?php
/**
 * 功能: 浏览记录
 * User: 文丹
 * Date: 2018/8/3 0003
 * Time: 14:35
 */

namespace app\index\model;
use think\Db;
use think\Model;

class Browse extends Model{
    /**
     * 查询浏览记录
     * @param $user_id
     * @param $goods_id
     * @return array|false|mixed|\PDOStatement|string|Model
     */
    public function getBrowse($user_id,$goods_id,$goods_end_time){
            $result = Db::name('browse')
            ->where('user_id',$user_id)
            ->where('goods_id',$goods_id)
            ->where('goods_end_time',$goods_end_time)
            ->find();
        return $result;
    }

    /**
     * 添加浏览记录
     * @param $data
     * @return int|string
     */
    public function add($data){
        $result = Db::name('browse')->insert($data);
        return $result;
    }

    public function saveBrowse($data,$user_id,$goods_id){
        $result = Db::name('browse')
            ->where('user_id',$user_id)
            ->where('goods_id',$goods_id)
            ->update($data);
        return $result;
    }

    /**
     * 展示浏览记录
     * @param $user_id
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
    public function showBrowse($user_id){
        $result = Db::name('browse')
            ->where('user_id',$user_id)
            ->order('time desc')
            ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                $goods = Db::name('goods')->where('goods_id',$item['goods_id'])->find();
                if($goods){
                    $item['goods_exist'] = 1;
                }else{
                    $item['goods_exist'] = 0;
                }
                return $item;
            });
       // $arr = [];
        foreach($result as $k=>$v){
            $arr[$k] = $v;
            $goods = Db::name('goods')->where('goods_id',$v['goods_id'])->find();
            if($goods){
                $arr[$k]['goods_exist'] = 1;
            }else{
                $arr[$k]['goods_exist'] = 0;
            }
           /* $arr[$k]['goods_title'] = $goods['goods_title'];
            $arr[$k]['goods_end_time'] = $goods['goods_end_time'];
            $arr[$k]['goods_end_time'] = $goods['goods_end_time'];*/

        }
        return $result;
    }

}